<?php

namespace app\store\controller;

use app\store\model\UserMonthCardModel;
use app\store\model\UserPropLogModel;
use app\store\model\UserScoreLogModel;
use app\store\model\UserSignLogModel;
use library\Controller;

/**
 * 日志管理
 */
class Log extends Controller
{

    public function __construct()
    {
        parent::__construct();
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        $this->assign('channel_arr',$channel_arr);
    }

    /**
     * @throws \think\exception\DbException
     */
    public function system()
    {
        $this->title = '系统数据';

        //破产数据
        $user_score_log = new UserScoreLogModel();
        $bankrupt_data = $user_score_log->search();
        $start_time = date('Y-m-d');

        $this->assign('start_time',$start_time);
        $this->assign('bankrupt_data',$bankrupt_data);

        $this->fetch();
    }

    /**道具统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function prop()
    {
        $this->title = '道具统计';
        $user_prop_log = new UserPropLogModel();
        $prop_data = $user_prop_log->getProp($this->structure_where());
        $start_time =date('Y-m-d');
        $this->assign('start_time',$start_time);
        $this->assign('prop_data',$prop_data);
        $this->fetch();
    }

    public function sign_in()
    {
        $this->title = '签到统计';
        $data = (new UserSignLogModel())->getSignIn($this->structure_where());
        $start_time = date('Y-m-d');

        $this->assign('start_time',$start_time.' - '.$start_time);
        $this->assign('data',$data);
        $this->fetch();
    }

    public function month_card()
    {
        $this->title = '月卡统计';

        $data = (new UserMonthCardModel())->getMonthCard($this->structure_where());
        $start_time =date('Y-m-d');
        $this->assign('start_time',$start_time);
        $this->assign('data',$data);
        $this->fetch();
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dan_tou()
    {
        $this->title = '弹头统计';

        $data = (new UserPropLogModel())->getDanTou($this->structure_where());
        $start_time =date('Y-m-d');
        $this->assign('start_time',$start_time);
        $this->assign('data',$data);
        $this->fetch();
    }


    protected function structure_where()
    {
        $user_field = $this->request->param('user_field','');
        $user_value_start = $this->request->param('user_value_start','');
        $user_value_end = $this->request->param('user_value_end','');

        // 如果只传了一个值 就把这个值赋给另外一个值
        if ($user_value_start != '' and $user_value_end == '') {
            $user_value_end = $user_value_start;
        }elseif ($user_value_start == '' and $user_value_end != '') {
            $user_value_start = $user_value_end;
        }

        $where = [];
        if($user_field != '' and $user_value_start != '' and $user_value_end != ''){
            // 如果只传了一个值
            if($user_value_end == $user_value_start){
                if($user_field == 'cannon'){
                    $where['g.Cannon'] = $user_value_start;
                }else{
                    $where['a.'.$user_field] = $user_value_start;
                }
            }else{
                if($user_field == 'cannon'){
                    $where[] = ['g.Cannon','between',[$user_value_start,$user_value_end]];
                }else{
                    $where[] = ['a.'.$user_field,'between',[$user_value_start,$user_value_end]];
                }
            }

        }
        return $where;
    }


    // 道具专用时间筛选
    protected function structure_where_time()
    {
        $time_field = $this->request->param('time_field');
        $time_value = $this->request->param('time_value');

        $where = [];

        if(!empty($time_field) and !empty($time_value)){
            $where[$time_field][] = $time_value;
            $where[$time_field][] = $time_value.' 23:59:59';

        }else{
            $where[$time_field][] = date('Y-m-d');
            $where[$time_field][] = date('Y-m-d 23:59:59');
        }

        return $where;
    }

    /**
     * 添加商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 禁用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
